﻿namespace jzgl.Windows
{
    using jzgl.Controller;
    using jzgl.Model;
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;

    public class MaterTypeInfo : Form
    {
        private Button btn_QueDing;
        private Button btn_QuXiao;
        private Button btn_ShanChu;
        private Button btn_TianJia;
        private Button btn_XiuGai;
        private Button btnDaoru;
        private DataGridViewTextBoxColumn Column1;
        private DataGridViewTextBoxColumn Column2;
        private DataGridViewTextBoxColumn Column3;
        private IContainer components = null;
        private DataGridView dataGridView1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn1;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn2;
        private DataGridViewTextBoxColumn dataGridViewTextBoxColumn3;
        private GroupBox groupBox1;
        private GroupBox groupBox2;
        private GroupBox groupBox3;
        private MaterInfoDB infoDB = new MaterInfoDB();
        private Label label1;
        private Label label2;
        private OpenFileDialog openFileDialog1;
        private Panel panel1;
        private int t = 0;
        private DataTable table = null;
        private TextBox txt_name;
        private TextBox txt_remark;
        private MaterType type = new MaterType();
        private MaterTypeDB typeDB = new MaterTypeDB();

        public MaterTypeInfo()
        {
            this.InitializeComponent();
            this.Data_bind();
            this.groupBox2.Visible = true;
            this.groupBox3.Visible = false;
        }

        private void btn_QueDing_Click(object sender, EventArgs e)
        {
            try
            {
                this.type._MaterTypeName = this.txt_name.Text.Trim();
                this.type._MaterTypeRemark = this.txt_remark.Text.Trim();
                if ((this.t == 1) && (this.typeDB.Add_MaterType(this.type) != 0))
                {
                    MessageBox.Show("已添加！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    this.t = 0;
                }
                if (this.t == 2)
                {
                    this.type._MaterTypeId = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString());
                    if (this.typeDB.Update_MaterType(this.type) != 0)
                    {
                        MessageBox.Show("修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.t = 0;
                    }
                }
                this.groupBox2.Visible = true;
                this.groupBox3.Visible = false;
                this.Data_bind();
                this.dataGridView1.Enabled = true;
            }
            catch (Exception)
            {
            }
        }

        private void btn_QuXiao_Click(object sender, EventArgs e)
        {
            try
            {
                this.groupBox3.Visible = false;
                this.groupBox2.Visible = true;
                this.txt_remark.Text = this.txt_name.Text = "";
                this.dataGridView1.Enabled = true;
            }
            catch (Exception)
            {
            }
        }

        private void btn_ShanChu_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim().Equals("") || (this.dataGridView1.Rows.Count == 0))
                {
                    MessageBox.Show("请选择要删除的数据行！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else if (MessageBox.Show("如果要删除此类别则所属此类别\n的所有物料信息也将被删除\n确定要删除吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.type._MaterTypeId = int.Parse(this.dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim());
                    this.infoDB.Delete_MaterInfoBY_type_Id(this.type._MaterTypeId);
                    if (this.typeDB.Delete_MaterType(this.type) != 0)
                    {
                        MessageBox.Show("已删除！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                        this.Data_bind();
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        private void btn_ShuaXin_Click(object sender, EventArgs e)
        {
            try
            {
                this.dataGridView1.Rows.Clear();
                this.Data_bind();
                this.txt_name.Text = this.txt_remark.Text = "";
                this.dataGridView1.Enabled = true;
            }
            catch (Exception)
            {
            }
        }

        private void btn_TianJia_Click(object sender, EventArgs e)
        {
            try
            {
                this.t = 1;
                this.groupBox2.Visible = true;
                this.groupBox3.Visible = true;
                this.txt_remark.Text = this.txt_name.Text = "";
                this.dataGridView1.Enabled = false;
            }
            catch (Exception)
            {
            }
        }

        private void btn_XiuGai_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.Rows.Count == 0)
                {
                    MessageBox.Show("没有数据修改！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                else
                {
                    this.t = 2;
                    this.groupBox2.Visible = true;
                    this.groupBox3.Visible = true;
                    this.dataGridView1.Enabled = true;
                    this.txt_name.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString().Trim();
                    this.txt_remark.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString().Trim();
                }
            }
            catch (Exception)
            {
            }
        }

        private void btnDaoru_Click(object sender, EventArgs e)
        {
            try
            {
                if (MessageBox.Show("确定要导入类别信息吗？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.openFileDialog1.Filter = "Execl files (*.xls)|*.xls";
                    this.openFileDialog1.FilterIndex = 0;
                    this.openFileDialog1.RestoreDirectory = true;
                    this.openFileDialog1.Title = "导入物料类别文件";
                    if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                    {
                        string fileName = this.openFileDialog1.FileName;
                        string str2 = fileName.Substring(fileName.LastIndexOf(".")).ToString().Trim();
                        string str3 = fileName.Substring(fileName.LastIndexOf(@"\")).ToString().Trim();
                        string filename = str3.Substring(1, str3.IndexOf(".") - 1);
                        if (str2 != ".xls")
                        {
                            MessageBox.Show("文件格式不正确！");
                        }
                        else
                        {
                            DataSet set = null;
                            set = ConnDB.ExcelToDS(this.openFileDialog1.FileName, filename);
                            if (set != null)
                            {
                                if (set.Tables[0].Rows.Count > 0)
                                {
                                    DataTable table = set.Tables[0];
                                    if (table.Columns.Count >= 1)
                                    {
                                        int num2 = 0;
                                        this.table = this.typeDB.Show_MaterType();
                                        int num3 = this.typeDB.Delete_MaterType();
                                        for (int i = 1; i < table.Rows.Count; i++)
                                        {
                                            int num6;
                                            for (int j = 0; j < this.table.Rows.Count; j++)
                                            {
                                                if (table.Rows[i][0].ToString().Trim().Equals(this.table.Rows[j][1].ToString().Trim()))
                                                {
                                                    num2 = 1;
                                                    this.type._MaterTypeId = int.Parse(this.table.Rows[j][0].ToString());
                                                    this.type._MaterTypeName = table.Rows[i][0].ToString();
                                                    this.type._MaterTypeRemark = table.Rows[i][1].ToString();
                                                    num6 = this.typeDB.Update_MaterType(this.type);
                                                    break;
                                                }
                                            }
                                            if (num2 == 0)
                                            {
                                                this.type._MaterTypeName = table.Rows[i][0].ToString();
                                                this.type._MaterTypeRemark = table.Rows[i][1].ToString();
                                                num6 = this.typeDB.Add_MaterType(this.type);
                                            }
                                            num2 = 0;
                                        }
                                    }
                                }
                                this.Data_bind();
                            }
                            else
                            {
                                MessageBox.Show("导入失败，请检查：\n数据源是否为空 \n数据源格式是否正确 \n文件名是否修改", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }

        public void Data_bind()
        {
            try
            {
                this.dataGridView1.Rows.Clear();
                this.table = this.typeDB.Show_MaterType();
                if (this.table != null)
                {
                    for (int i = 0; i < this.table.Rows.Count; i++)
                    {
                        object[] values = new object[] { this.table.Rows[i][0].ToString(), this.table.Rows[i][1].ToString(), this.table.Rows[i][2].ToString() };
                        this.dataGridView1.Rows.Add(values);
                    }
                }
                this.txt_name.Text = this.txt_remark.Text = "";
            }
            catch (Exception)
            {
            }
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                this.txt_name.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
                this.txt_remark.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            }
            catch (Exception)
            {
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (this.components != null))
            {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.panel1 = new Panel();
            this.groupBox2 = new GroupBox();
            this.groupBox3 = new GroupBox();
            this.txt_remark = new TextBox();
            this.txt_name = new TextBox();
            this.btn_QuXiao = new Button();
            this.btn_QueDing = new Button();
            this.label2 = new Label();
            this.label1 = new Label();
            this.btn_ShanChu = new Button();
            this.btn_XiuGai = new Button();
            this.btn_TianJia = new Button();
            this.btnDaoru = new Button();
            this.groupBox1 = new GroupBox();
            this.dataGridView1 = new DataGridView();
            this.Column1 = new DataGridViewTextBoxColumn();
            this.Column2 = new DataGridViewTextBoxColumn();
            this.Column3 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn1 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn2 = new DataGridViewTextBoxColumn();
            this.dataGridViewTextBoxColumn3 = new DataGridViewTextBoxColumn();
            this.openFileDialog1 = new OpenFileDialog();
            this.panel1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.groupBox1.SuspendLayout();
            ((ISupportInitialize) this.dataGridView1).BeginInit();
            base.SuspendLayout();
            this.panel1.Controls.Add(this.groupBox2);
            this.panel1.Controls.Add(this.groupBox1);
            this.panel1.Location = new Point(0, 1);
            this.panel1.Name = "panel1";
            this.panel1.Size = new Size(0x1c3, 0x1d2);
            this.panel1.TabIndex = 0;
            this.groupBox2.Controls.Add(this.groupBox3);
            this.groupBox2.Controls.Add(this.btn_ShanChu);
            this.groupBox2.Controls.Add(this.btn_XiuGai);
            this.groupBox2.Controls.Add(this.btn_TianJia);
            this.groupBox2.Controls.Add(this.btnDaoru);
            this.groupBox2.Location = new Point(3, 0x185);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new Size(0x1bd, 0x3f);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox3.Controls.Add(this.txt_remark);
            this.groupBox3.Controls.Add(this.txt_name);
            this.groupBox3.Controls.Add(this.btn_QuXiao);
            this.groupBox3.Controls.Add(this.btn_QueDing);
            this.groupBox3.Controls.Add(this.label2);
            this.groupBox3.Controls.Add(this.label1);
            this.groupBox3.Location = new Point(0, 0);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new Size(0x1bd, 0x3f);
            this.groupBox3.TabIndex = 2;
            this.groupBox3.TabStop = false;
            this.txt_remark.Location = new Point(0xd4, 11);
            this.txt_remark.Name = "txt_remark";
            this.txt_remark.Size = new Size(0xc4, 0x15);
            this.txt_remark.TabIndex = 5;
            this.txt_name.Location = new Point(40, 11);
            this.txt_name.Name = "txt_name";
            this.txt_name.Size = new Size(0x84, 0x15);
            this.txt_name.TabIndex = 4;
            this.btn_QuXiao.Location = new Point(0xd4, 0x24);
            this.btn_QuXiao.Name = "btn_QuXiao";
            this.btn_QuXiao.Size = new Size(0x4b, 0x17);
            this.btn_QuXiao.TabIndex = 3;
            this.btn_QuXiao.Text = "取消(&C)";
            this.btn_QuXiao.UseVisualStyleBackColor = true;
            this.btn_QuXiao.Click += new EventHandler(this.btn_QuXiao_Click);
            this.btn_QueDing.Location = new Point(0x83, 0x24);
            this.btn_QueDing.Name = "btn_QueDing";
            this.btn_QueDing.Size = new Size(0x4b, 0x17);
            this.btn_QueDing.TabIndex = 2;
            this.btn_QueDing.Text = "确定(&O)";
            this.btn_QueDing.UseVisualStyleBackColor = true;
            this.btn_QueDing.Click += new EventHandler(this.btn_QueDing_Click);
            this.label2.AutoSize = true;
            this.label2.Location = new Point(0xb2, 15);
            this.label2.Name = "label2";
            this.label2.Size = new Size(0x29, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "备注：";
            this.label1.AutoSize = true;
            this.label1.Location = new Point(5, 15);
            this.label1.Name = "label1";
            this.label1.Size = new Size(0x29, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "名称：";
            this.btn_ShanChu.Location = new Point(320, 0x11);
            this.btn_ShanChu.Name = "btn_ShanChu";
            this.btn_ShanChu.Size = new Size(0x4b, 0x17);
            this.btn_ShanChu.TabIndex = 2;
            this.btn_ShanChu.Text = "删除(&D)";
            this.btn_ShanChu.UseVisualStyleBackColor = true;
            this.btn_ShanChu.Click += new EventHandler(this.btn_ShanChu_Click);
            this.btn_XiuGai.Location = new Point(0xe9, 0x11);
            this.btn_XiuGai.Name = "btn_XiuGai";
            this.btn_XiuGai.Size = new Size(0x4b, 0x17);
            this.btn_XiuGai.TabIndex = 1;
            this.btn_XiuGai.Text = "修改(&E)";
            this.btn_XiuGai.UseVisualStyleBackColor = true;
            this.btn_XiuGai.Click += new EventHandler(this.btn_XiuGai_Click);
            this.btn_TianJia.Location = new Point(0x90, 0x11);
            this.btn_TianJia.Name = "btn_TianJia";
            this.btn_TianJia.Size = new Size(0x4b, 0x17);
            this.btn_TianJia.TabIndex = 0;
            this.btn_TianJia.Text = "添加(&A)";
            this.btn_TianJia.UseVisualStyleBackColor = true;
            this.btn_TianJia.Click += new EventHandler(this.btn_TianJia_Click);
            this.btnDaoru.Location = new Point(0x34, 0x11);
            this.btnDaoru.Name = "btnDaoru";
            this.btnDaoru.Size = new Size(0x54, 0x17);
            this.btnDaoru.TabIndex = 3;
            this.btnDaoru.Text = "导入类别(&I)";
            this.btnDaoru.UseVisualStyleBackColor = true;
            this.btnDaoru.Click += new EventHandler(this.btnDaoru_Click);
            this.groupBox1.Controls.Add(this.dataGridView1);
            this.groupBox1.Location = new Point(3, 3);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(0x1bd, 380);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "物料类别管理";
            this.dataGridView1.AllowUserToAddRows = false;
            this.dataGridView1.BackgroundColor = SystemColors.ControlLightLight;
            this.dataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Columns.AddRange(new DataGridViewColumn[] { this.Column1, this.Column2, this.Column3 });
            this.dataGridView1.Location = new Point(5, 0x15);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.ReadOnly = true;
            this.dataGridView1.RowHeadersWidth = 30;
            this.dataGridView1.RowTemplate.Height = 0x17;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            this.dataGridView1.Size = new Size(0x1b2, 0x161);
            this.dataGridView1.TabIndex = 0;
            this.dataGridView1.CellClick += new DataGridViewCellEventHandler(this.dataGridView1_CellClick);
            this.Column1.HeaderText = "类别编号";
            this.Column1.Name = "Column1";
            this.Column1.ReadOnly = true;
            this.Column1.Visible = false;
            this.Column1.Width = 0x55;
            this.Column2.HeaderText = "类别名称";
            this.Column2.Name = "Column2";
            this.Column2.ReadOnly = true;
            this.Column2.Width = 190;
            this.Column3.HeaderText = "备注说明";
            this.Column3.Name = "Column3";
            this.Column3.ReadOnly = true;
            this.Column3.Width = 210;
            this.dataGridViewTextBoxColumn1.HeaderText = "类别编号";
            this.dataGridViewTextBoxColumn1.Name = "dataGridViewTextBoxColumn1";
            this.dataGridViewTextBoxColumn1.Visible = false;
            this.dataGridViewTextBoxColumn1.Width = 0x55;
            this.dataGridViewTextBoxColumn2.HeaderText = "名称";
            this.dataGridViewTextBoxColumn2.Name = "dataGridViewTextBoxColumn2";
            this.dataGridViewTextBoxColumn2.Width = 180;
            this.dataGridViewTextBoxColumn3.HeaderText = "备注说明";
            this.dataGridViewTextBoxColumn3.Name = "dataGridViewTextBoxColumn3";
            this.dataGridViewTextBoxColumn3.Width = 190;
            this.openFileDialog1.FileName = "openFileDialog1";
            base.AutoScaleDimensions = new SizeF(6f, 12f);
           // base.AutoScaleMode = AutoScaleMode.Font;
            base.ClientSize = new Size(0x1c5, 0x1d5);
            base.Controls.Add(this.panel1);
            //base.FormBorderStyle = FormBorderStyle.FixedSingle;
            base.MaximizeBox = false;
            this.MaximumSize = new Size(0x1cb, 0x1f5);
            this.MinimumSize = new Size(0x1cb, 0x1f5);
            base.Name = "MaterTypeInfo";
            this.Text = "物料类别管理";
            this.panel1.ResumeLayout(false);
            this.groupBox2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.groupBox1.ResumeLayout(false);
            ((ISupportInitialize) this.dataGridView1).EndInit();
            base.ResumeLayout(false);
        }
    }
}

